package com.jn.server;

import javax.swing.*;
import java.awt.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.FutureTask;

/**
 * @author 江南大学1033190417
 * @date 2022/4/7 19:56
 */
public class View extends JFrame {
    ServerSocket server = null;
    int i = 1;

    private JTextField port;//端口号设置
    private JTextField path;//路径设置

    private JButton start;//开始
    private JButton quit;//结束

    private JTextArea lianjie;

    private JLabel fasong;
    private JLabel shoudao;

    public View() {
        super("马帅的服务器");
        Container container = getContentPane();
        container.setLayout(new FlowLayout());


        JLabel l1 = new JLabel("端 口 号：");
        port = new JTextField(15);
        port.setText("8090");
        container.add(l1);
        container.add(port);

        JLabel l2 = new JLabel("指定路径：");
        path = new JTextField(15);
        path.setText("D:\\桌面\\");
        container.add(l2);
        container.add(path);


        start = new JButton("开始");
        quit = new JButton("退出");
        container.add(start);
        container.add(quit);

        lianjie = new JTextArea(25, 69);
        lianjie.setText("输出结果:\n");
        lianjie.setFont(new Font("楷体", Font.BOLD, 21));
        JScrollPane pane = new JScrollPane(lianjie);
        pane.setSize(28, 69);
        container.add(pane);

        JLabel l3 = new JLabel("发送：");
        fasong = new JLabel("0");
        JLabel l4 = new JLabel("                  收到：");
        shoudao = new JLabel("0");
        container.add(l3);
        container.add(fasong);
        container.add(l4);
        container.add(shoudao);


        start.addActionListener(
                action -> {
                    int PORT = Integer.parseInt(port.getText());
                    String paths = path.getText();
                    try {
                        server = new ServerSocket(PORT);
                        lianjie.append("服务启动：" + server.getInetAddress().toString() + "\t" + server.getLocalPort() + "\n\n");
                        Thread thread = new Thread(() -> {
                            while (!server.isClosed()) {
                                try {
                                    Socket client = server.accept();
                                    //接收客户端的连接请求
                                    new ConnectionThread(client, i, paths,lianjie,fasong,shoudao).start();
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    lianjie.append("\n异常1：\n" + ex + "\n");
                                }
                            }
                        });
                        thread.setName("马帅_网络服务器\n\n");
                        lianjie.append("服务器名: " + thread.getName());
                        thread.start();
                    } catch (Exception e) {
                        e.printStackTrace();
                        lianjie.append("\n异常2：\n" + e + "\n");
                    }

                }
        );

        quit.addActionListener(
                e -> {
                    try {
                        lianjie.append("服务关闭：" + server.getInetAddress().toString() + "\t" + server.getLocalPort() + "\n\n");
                        server.close();
                        i = 1;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        lianjie.append("\n异常：\n" + e1);
                    }
                }
        );

        Component[] components = container.getComponents();
        for (Component component : components) {
            component.setFont(new Font("楷体", Font.BOLD, 21));
        }
        setSize(900, 800);
        setVisible(true);
    }

}
